﻿using System;
using System.Windows.Forms;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using YArchitech.Revit;

namespace HYFloorPipeModule
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class FloorPipeMarkingCommand : HYExternalCommand
	{
		public override Result HYExecute(ref string TranName)
		{
			TranName = "标注";
			Result result;
			try
			{
				for (;;)
				{
					Reference reference = base.Sel.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, new HYModelLineGroupFilter(), "请选择一个地热盘管");
					Element element = base.Doc.GetElement(reference);
					FloorPipeTools floorPipeTools = new FloorPipeTools(base.Doc);
					FloorPipeGroupData elementES = floorPipeTools.GetElementES(element);
					double floorPipeDistance = floorPipeTools.GetFloorPipeDistance(elementES);
					double length = Math.Round(elementES.PipesLength, 1);
					FormFloorPipeMarking formFloorPipeMarking = new FormFloorPipeMarking(floorPipeDistance, length);
					for (;;)
					{
						DialogResult dialogResult = formFloorPipeMarking.ShowDialog();
						if (dialogResult == DialogResult.Abort)
						{
							reference = base.Sel.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, new HYModelLineGroupFilter(), "请选择一个地热盘管");
							element = base.Doc.GetElement(reference);
							elementES = floorPipeTools.GetElementES(element);
							floorPipeDistance = floorPipeTools.GetFloorPipeDistance(elementES);
							length = Math.Round(elementES.PipesLength, 1);
							formFloorPipeMarking.InitParaDataSetting(floorPipeDistance, length);
						}
						else
						{
							if (dialogResult == DialogResult.OK)
							{
								break;
							}
							if (dialogResult == DialogResult.Cancel)
							{
								goto Block_4;
							}
						}
					}
					XYZ xyz = base.Sel.PickPoint("请选择标注引线的起始点");
					QuadrantLocation pointQuadrant = QuadrantLocation.First;
					XYZ xyz2 = null;
					using (FloorPipeDynamicDrag floorPipeDynamicDrag = new FloorPipeDynamicDrag(base.UiDoc))
					{
						floorPipeDynamicDrag.StartPoint = xyz;
						xyz2 = base.Sel.PickPoint("请选择标注引线的终点");
						while (xyz.IsAlmostEqualTo(xyz2))
						{
							xyz2 = base.Sel.PickPoint("起点和终点不能为同一个点，请重新选择标注引线的终点");
						}
						pointQuadrant = floorPipeDynamicDrag.PointQuadrant;
					}
					FloorPipeMarkingTool.MarkFloorPipes(base.Doc, xyz, xyz2, formFloorPipeMarking.Length, formFloorPipeMarking.Distance, pointQuadrant);
				}
				Block_4:
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			return result;
		}
	}
}
